import ipc from '~/lib/ipc.coffee'
import wh from '~/lib/wh.coffee'
{recbar} = ipc

export default =>
  {width,height} = screen
  [w,h] = wh(
    width * 2
    height * 2
  )

  localStorage.ppi = w/width

  src = {
    audio:
      mandatory:
        chromeMediaSource: "desktop"
    video:
      mandatory:
        # TODO 多屏幕支持
        maxFrameRate:30
        minWidth: w
        minHeight: h
        maxWidth: w
        maxHeight: h
        chromeMediaSourceId:await recbar.screen_source()
        chromeMediaSource: "desktop"
  }

  get = =>
    navigator.mediaDevices.getUserMedia src

  try
    s = await get()
  catch err
    delete src.audio
    s = await get()
  s
